package com.d1711.zhanggm.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.d1711.zhanggm.springboot.entity.*;
import com.d1711.zhanggm.springboot.mapper.StudentMapper;
import com.d1711.zhanggm.springboot.service.IAreaService;
import com.d1711.zhanggm.springboot.service.IHobbyService;
import com.d1711.zhanggm.springboot.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.d1711.zhanggm.springboot.service.ISudentHobbyRelationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 学生表 服务实现类
 * </p>
 *
 * @author 张国明
 * @since 2020-05-27
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Autowired
    private ISudentHobbyRelationService iSudentHobbyRelationService;

    @Autowired
    private IHobbyService iHobbyService;

    @Autowired
    private IAreaService iAreaService;

    @Override
    public PageInfo<StudentVo> getPageInfo(StudentVo studentVo, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<StudentVo> students = this.getBaseMapper().selectList(studentVo);
        students.forEach(stuvo -> {
            //设置兴趣爱好
            stuvo.setHobbyNames(getHobbyNamesByStuId(stuvo.getId()));
            //设置省
            Area provice = iAreaService.getById(stuvo.getProviceId());
            stuvo.setProviceName(provice.getName());
            //设置市
            Area city = iAreaService.getById(stuvo.getCityId());
            stuvo.setCityName(city.getName());
            //设置区
            Area area = iAreaService.getById(stuvo.getAreaId());
            stuvo.setAreaName(area.getName());
        });
        return new PageInfo<>(students);
    }

    /**
     * 根据学生Id，查询所有爱好
     * @param stuId
     * @return
     */
    private String getHobbyNamesByStuId(Integer stuId){
        final StringBuffer hobbyNames = new StringBuffer();
        //查询关系数据
       /* QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("student_id",stuId);
        List<SudentHobbyRelation> relationList = iSudentHobbyRelationService.list(queryWrapper);
        //根据爱好Id查询爱好表
        List<Integer> hobbyIdList = relationList.stream().map(relation -> relation.getHobbyId()).collect(Collectors.toList());*/
        //relationList.stream().map(relation -> relation.getHobbyId()).collect(Collectors.toList());
        List<Integer> hobbyIdList = iSudentHobbyRelationService.getHobbyIdListByStuId(stuId);
        //查询爱好
        if(hobbyIdList!=null && hobbyIdList.size()>0){
            List<Hobby> hobbies = iHobbyService.listByIds(hobbyIdList);
            hobbies.forEach(hobby -> {
                if("".equals(hobbyNames.toString())){
                    hobbyNames.append(hobby.getName());
                }else {
                    hobbyNames.append("/").append(hobby.getName());
                }
            });
        }
        return hobbyNames.toString();
    }


}
